//
//  HomeTableViewCell.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/25.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "HomeTableViewCell.h"

@interface HomeTableViewCell()
@property (nonatomic, strong)UILabel * lendRateLab;//出借率
@property (nonatomic, strong)UILabel * lendTitleLab;//出借标题
@property (nonatomic, strong)UILabel * projectLab;//项目名称
@property (nonatomic, strong)UILabel * timeLimitsLab;//期限
@property (nonatomic, strong)UILabel * investmentVolumeLab;//投资额
@property (nonatomic, strong)NSString * bidID;//标的ID
@end

@implementation HomeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        [self.contentView addSubview:self.lendRateLab];
        [self.contentView addSubview:self.lendTitleLab];
        [self.contentView addSubview:self.projectLab];
        [self.contentView addSubview:self.timeLimitsLab];
        [self.contentView addSubview:self.investmentVolumeLab];
        UIView * topLineView = [[UIView alloc] init];
        topLineView.backgroundColor = getColor(grayColor);
        [self.contentView addSubview:topLineView];
        [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        UIView * bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = getColor(grayColor);
        [self.contentView addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(weakSelf);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        [self.lendRateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(23/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.mas_top).mas_offset(24/WIDTH_6_SCALE);
            make.height.mas_offset(20/WIDTH_6_SCALE);
        }];
        [self.lendTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(18/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.lendRateLab.mas_bottom).mas_offset(14/WIDTH_6_SCALE);
            make.height.mas_offset(12/WIDTH_6_SCALE);
        }];
        [self.projectLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.lendRateLab.mas_top);
            make.height.mas_offset(13/WIDTH_6_SCALE);
            make.left.mas_offset(140/WIDTH_6_SCALE);
        }];
        [self.timeLimitsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.lendTitleLab.mas_bottom);
            make.height.equalTo(weakSelf.lendTitleLab.mas_height).mas_offset(5/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.lendTitleLab.mas_right).mas_offset(32/WIDTH_6_SCALE);
        }];
        [self.investmentVolumeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.lendTitleLab.mas_bottom);
            make.height.equalTo(weakSelf.timeLimitsLab.mas_height);
            make.left.equalTo(weakSelf.timeLimitsLab.mas_right).mas_offset(32/WIDTH_6_SCALE);
        }];
//        self.lendTitleLab.backgroundColor = [UIColor orangeColor];
//        self.timeLimitsLab.backgroundColor = [UIColor orangeColor];
//        self.investmentVolumeLab.backgroundColor = [UIColor orangeColor];
    }return self;
}
#pragma mark - init
- (UILabel *)lendRateLab{
    if (!_lendRateLab) {
        _lendRateLab = [[UILabel alloc] init];
        _lendRateLab.font = DEF_FontSize_10;
        _lendRateLab.textColor = [UIColor orangeColor];
        _lendRateLab.textAlignment = NSTextAlignmentLeft;
        
    }return _lendRateLab;
}

- (UILabel *)lendTitleLab{
    if (!_lendTitleLab) {
        _lendTitleLab = [[UILabel alloc] init];
        _lendTitleLab.font = DEF_FontSize_10;
        _lendTitleLab.textColor = getColor(@"959595");
        _lendTitleLab.textAlignment = NSTextAlignmentLeft;
        _lendTitleLab.text = @"平台建议年均出借率";
    }return _lendTitleLab;
}

- (UILabel *)projectLab{
    if (!_projectLab) {
        _projectLab = [[UILabel alloc] init];
        _projectLab.font = DEF_FontSize_12;
        _projectLab.textColor = getColor(@"737373");
        _projectLab.textAlignment = NSTextAlignmentLeft;
//        _projectLab.text = @"【家家泰365天J16062141】";
    }return _projectLab;
}

- (UILabel *)timeLimitsLab{
    if (!_timeLimitsLab) {
        _timeLimitsLab = [[UILabel alloc] init];
        _timeLimitsLab.font = DEF_FontSize_10;
        _timeLimitsLab.textColor = getColor(@"959595");
        _timeLimitsLab.textAlignment = NSTextAlignmentLeft;
//        _timeLimitsLab.text = @"投标期限90天";
//        //改变字体
//        NSString * timeLimitsStr = @"投标期限90天";
//        [self changeStringTypeface:timeLimitsStr withFirstLocStr:@"限" withSecondLocStr:@"天" withLabel:_timeLimitsLab];
    }return _timeLimitsLab;
}

- (UILabel *)investmentVolumeLab{
    if (!_investmentVolumeLab) {
        _investmentVolumeLab = [[UILabel alloc] init];
        _investmentVolumeLab.font = DEF_FontSize_10;
        _investmentVolumeLab.textColor = getColor(@"959595");
        _investmentVolumeLab.textAlignment = NSTextAlignmentLeft;
//        _investmentVolumeLab.text = @"剩余投标额1万元";
//        //改变字体
//        NSString * investmentVolumeStr = @"剩余投标额1万元";
//        [self changeStringTypeface:investmentVolumeStr withFirstLocStr:@"额" withSecondLocStr:@"万" withLabel:_investmentVolumeLab];
    }return _investmentVolumeLab;
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    //出借率
    NSString * lendRateStr = [NSString stringWithFormat:@"%@%%",model[@"lolimit"]];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:lendRateStr];
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = 0;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:@"%"].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [self.lendRateLab setAttributedText:noteStr];
    //投标期限
    NSString * timeLimitsStr = [NSString stringWithFormat:@"投标期限%@天", model[@"invtime"]];
    [self changeStringTypeface:timeLimitsStr withFirstLocStr:@"限" withSecondLocStr:@"天" withLabel:self.timeLimitsLab];
    //剩余投标额
    NSString * investmentVolumeStr = [NSString stringWithFormat:@"剩余投标额%@万元", model[@"sursum"]];
    [self changeStringTypeface:investmentVolumeStr withFirstLocStr:@"额" withSecondLocStr:@"万" withLabel:self.investmentVolumeLab];
    //项目名
    NSString * projectStr = [NSString stringWithFormat:@"【%@】", model[@"entitle"]];
    self.projectLab.text = projectStr;
    self.bidID = model[@"selectid"];
}
#pragma mark - 方法
//首字母就是数字的
- (void)changeStringTypeface:(NSString *)string withFirstLoc:(NSInteger)firstLoc withSecondLocStr:(NSString *)secondLocStr withLabel:(UILabel *)label{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
    //     需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:secondLocStr].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:nil size:18] range:range];
//    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [label setAttributedText:noteStr];
}

//首字母是字母的
- (void)changeStringTypeface:(NSString *)string withFirstLocStr:(NSString *)str withSecondLocStr:(NSString *)secondLocStr withLabel:(UILabel *)label{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:str].location + 1;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:secondLocStr].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:nil size:18] range:range];
//    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [label setAttributedText:noteStr];
}
@end
